package com.ruyuan.eshop.order.remote;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.market.api.MarketApi;
import com.ruyuan.eshop.market.domain.dto.CalculateOrderAmountDTO;
import com.ruyuan.eshop.market.domain.dto.UserCouponDTO;
import com.ruyuan.eshop.market.domain.query.UserCouponQuery;
import com.ruyuan.eshop.market.domain.request.CalculateOrderAmountRequest;
import com.ruyuan.eshop.market.domain.request.LockUserCouponRequest;
import com.ruyuan.eshop.market.domain.request.ReleaseUserCouponRequest;
import com.ruyuan.eshop.order.exception.OrderBizException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 营销服务远程接口
 * 儒猿课堂: http://www.ruyuan2020.com
 * @Author zhonghuashishan
 * @Remark 有问题直接联系客服VX: ruyuan-1111 ，源码-笔记-技术交流群
 * @Version 1.0
 **/
@Component
public class MarketRemote {

    /**
     * 营销服务
     */
    @DubboReference(version = "1.0.0", retries = 0)
    private MarketApi marketApi;

    /**
     * 计算订单费用
     *
     * @param calculateOrderPriceRequest
     * @return
     */
    @SentinelResource(value = "MarketRemote:calculateOrderAmount")
    public CalculateOrderAmountDTO calculateOrderAmount(CalculateOrderAmountRequest calculateOrderPriceRequest) {
        JsonResult<CalculateOrderAmountDTO> jsonResult = marketApi.calculateOrderAmount(calculateOrderPriceRequest);
        // 检查价格计算结果
        if (!jsonResult.getSuccess()) {
            throw new OrderBizException(jsonResult.getErrorCode(), jsonResult.getErrorMessage());
        }
        return jsonResult.getData();
    }

    /**
     * 锁定用户优惠券
     */
    @SentinelResource(value = "MarketRemote:lockUserCoupon")
    public Boolean lockUserCoupon(LockUserCouponRequest lockUserCouponRequest) {
        JsonResult<Boolean> jsonResult = marketApi.lockUserCoupon(lockUserCouponRequest);
        // 检查锁定用户优惠券结果
        if (!jsonResult.getSuccess()) {
            throw new OrderBizException(jsonResult.getErrorCode(), jsonResult.getErrorMessage());
        }
        return jsonResult.getData();
    }

    /**
     * 获取用户优惠券
     */
    public UserCouponDTO getUserCoupon(UserCouponQuery userCouponQuery) {
        JsonResult<UserCouponDTO> jsonResult = marketApi.getUserCoupon(userCouponQuery);
        if (jsonResult.getSuccess()) {
            return jsonResult.getData();
        }
        return null;
    }

    /**
     * 释放用户优惠券
     */
    @SentinelResource(value = "MarketRemote:releaseUserCoupon")
    public Boolean releaseUserCoupon(ReleaseUserCouponRequest releaseUserCouponRequest) {
        JsonResult<Boolean> jsonResult = marketApi.releaseUserCoupon(releaseUserCouponRequest);
        // 检查锁定用户优惠券结果
        if (!jsonResult.getSuccess()) {
            throw new OrderBizException(jsonResult.getErrorCode(), jsonResult.getErrorMessage());
        }
        return jsonResult.getData();
    }

}